NULL=

MAKEFLAGS=-j1

CURRENT_MAKEFILE:=$(lastword $(MAKEFILE_LIST))

CHECK_PRG=./scripts/check-program
SVG2PNG=./scripts/svg2png

LOGO_SIZES = \
	16 \
	22 \
	32 \
	48 \
	64 \
	128 \
	256 \
	512 \
	$(NULL)

LOGOS = \
	$(patsubst %,logos/out/karton-%.png,$(LOGO_SIZES)) \
	logos/out/favicon.ico \
	$(NULL)


########################################################################
# Misc generic targets                                                 #
########################################################################

# KEEP THIS THE FIRST TARGET OF THE MAKEFILE!
# There's no automatic target as we don't need to really build anything.
.PHONY: automatic-target
automatic-target:
	@echo "Nothing to build automatically!" >&2; exit 1

.PHONY: .FORCE
.FORCE:
	true

.PHONY: lint
lint:
	./scripts/lint.sh


########################################################################
# Docs                                                                 #
########################################################################

docs/props.md: karton/dockerfile.py scripts/props_docs.py
	./scripts/props_docs.py $@

.PHONY: docs
docs: docs/props.md


########################################################################
# Tests                                                                #
########################################################################

.PHONY: check
check: check-python2

.PHONY: check-python-all
check-python-all: check-python2 check-python3

.PHONY: check-python2
check-python2:
	@mkdir -p test-results/ 2> /dev/null
	python2 -m tests.run --save-json-results test-results/local.json

.PHONY: check-python3
check-python3:
	@mkdir -p test-results/ 2> /dev/null
	python3 -m tests.run --save-json-results test-results/local-python3.json

.PHONY: check-inception
check-inception: dist
	./tests/run-inception.sh dist/karton-`python ./karton/version.py`.tar.gz "$(TARGETS)" $(TESTS)

.PHONY: distcheck
distcheck:
	-$(MAKE) -f $(CURRENT_MAKEFILE) check-python2
	-$(MAKE) -f $(CURRENT_MAKEFILE) check-python3
	-$(MAKE) -f $(CURRENT_MAKEFILE) check-inception
	@echo
	@echo
	@echo "SUMMARY OF BOTH LOCAL AND INCEPTION TESTS"
	@echo "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
	@python ./tests/summary.py

.PHONY: distcheck-fast
distcheck-fast:
	@# Locally, we only run Python 3 tests as the Python2 ones will be run
	@# in inception.
	@rm test-results/local.json 2> /dev/null || true
	-$(MAKE) -f $(CURRENT_MAKEFILE) check-python3
	-$(MAKE) -f $(CURRENT_MAKEFILE) check-inception TARGETS="ubuntu:devel"
	@echo
	@echo
	@echo "SUMMARY OF FAST LOCAL AND INCEPTION TESTS"
	@echo "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
	@python ./tests/summary.py

.PHONY: rm-test-images
rm-test-images:
	-docker rmi --force \
	    $$( docker images -q --format '{{ .Repository }} {{ .ID }}' | \
	        grep '^karton-test-' | \
	        awk '{ print $$2 }' )
	-docker rmi --force \
	    $$( docker images --filter "dangling=true" --quiet )


########################################################################
# Distribution stuff                                                   #
########################################################################

.PHONY: dist
dist: MANIFEST.in
	python setup.py sdist --format=gztar

MANIFEST.in: .FORCE
	echo '# This file was generated by Makefile.' > $@
	echo '# DO NOT EDIT!' >> $@
	echo 'include MANIFEST.in' >> $@
	git ls-files | sed 's/^/include /' >> $@


########################################################################
# Logos                                                                #
########################################################################

.PHONY: logos
logos: $(LOGOS)

logos/out/.stamp:
	mkdir -p `dirname $@`
	touch $@

logos/out/karton-16.png: logos/src/karton-16.png logos/out/.stamp
	cp $< $@

logos/out/karton-22.png: logos/src/karton-22.png logos/out/.stamp
	cp $< $@

logos/out/karton-32.png: logos/src/karton-small.svg logos/out/.stamp
	$(SVG2PNG) $< $@ 32

logos/out/karton-48.png: logos/src/karton-small.svg logos/out/.stamp
	$(SVG2PNG) $< $@ 48

logos/out/karton-64.png: logos/src/karton.svg logos/out/.stamp
	$(SVG2PNG) $< $@ 64

logos/out/karton-128.png: logos/src/karton.svg logos/out/.stamp
	$(SVG2PNG) $< $@ 128

logos/out/karton-256.png: logos/src/karton.svg logos/out/.stamp
	$(SVG2PNG) $< $@ 256

logos/out/karton-512.png: logos/src/karton.svg logos/out/.stamp
	$(SVG2PNG) $< $@ 512

logos/out/favicon.ico: logos/out/karton-16.png logos/out/karton-32.png logos/out/.stamp
	@$(CHECK_PRG) convert
	convert \
	    \( logos/out/karton-16.png \) \
	    \( logos/out/karton-32.png \) \
	    $@
